#![no_std]
#![no_main]

extern crate panic_halt;

use cortex_m::asm;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprintln};
use cortex_m::peripheral::{syst};

use stm32f4::stm32f407::{Peripherals};

const SYSTICK_RELOAD_VALUE: u32 = 1_000;

mod frequency {
	pub const APB1: u32 = 8_000_000;
}

fn device_init(peripherals: &Peripherals) {
	cortex_m::interrupt::free(|cs| {
		// power up
		peripherals.RCC.ahb1enr.write(|w| w.gpioaen().set_bit());

		// configure pins
		peripherals.GPIOA.otyper.write(|w| w.ot12().clear_bit());
		peripherals.GPIOA.moder.write(|w| w.moder12().output());
		peripherals.GPIOA.pupdr.write(|w| w.pupdr12().pull_up());
	});

	let mut systick = peripherals.SYST;
	systick.set_clock_source(syst::SystClkSource::Core);
	systick.set_reload(SYSTICK_RELOAD_VALUE);
	systick.clear_current();
	systick.enable_counter();

	while !systick.has_wrapped() {
		// SysTick is initializing
	}
}

#[entry]
fn main() -> ! {
	hprintln!("SkyLab Version 0.0.1").unwrap();

	let peripherals = Peripherals::take().unwrap();
	device_init(&peripherals);

	loop {
		asm::nop();
	}
}
